Hi...
In this blog I will give you a small demonstration on LINQ to ARRAY. Here I will explain you that how to use LINQ for array to perform certain task such as sorting of array and searching records from array. Here I had created two methods in Program class named displayValueInOrder() which accept two parameters, one string type array which needs to be sorted and other one is string value which is either ASC or DESC. Second method is searchSingleRecord() which search a record from array and also accept two parameters one is string array on which searching needs to be perform and second one is searchVale which needs to be search.
Here is simple program which clear your concept about LINQ to array.
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace LinqDemo { class Program { /// <summary> /// This method display string array records either in ascending order /// or descending order on basis of order parameter. For performing /// this task I have use LINQ. We use orderby clause to specify ordering /// followed by variable name then order which is either ascending or /// descending. If you do not specify order then linq generate list /// in ascending order. /// </summary> public void displayValueInOrder(string[] records, string order) { if (order.Equals("ASC")) { var record = from std in records orderby std ascending select std;
//Displaying record in ascending order. foreach (string data in record) Console.WriteLine(data); } else { var record = from std in records orderby std descending select std;
//Displaying record in descending order. foreach (string data in record) Console.WriteLine(data); } }
/// <summary> /// This method search record from array and display first search value. /// searchSingleRecord() method takes two parameter first records array /// in which search operation needs to be perform and second one is /// search value which needs to be searched. After searching record /// it display first value if exists from search record. /// </summary> public void searchSingleRecord(string[] records,string searchValue) { string record = (from std in records where std.Equals(searchValue) select std).FirstOrDefault();
Console.WriteLine(record); }
static void Main(string[] args) { string[] studentName = { "Awadhendra", "Ankit", "James", "John", "Cat", "Shilpa", "Shivam", "Haider", "Ashish" };
Program pr = new Program(); pr.displayValueInOrder(studentName, "ASC");
Console.WriteLine(); pr.searchSingleRecord(studentName, "Avanish");
Console.ReadKey(); } } }
Thanks for reading this blog. Please provide any suggestion and feedback on this blog and suggest me some other topic in which I need to write articles or blogs.
Anonymous User
20-Apr-2019Thanks for the help.
James Smith
03-Oct-2011Chris Anderson
03-Oct-2011Wow! Its really solve my problem............
Thanks Awadhendra.
Anonymous User
26-Sep-2011Hi Aken,
Could you please explain your problem in more detail. What do you want to do with datagridview in this context.
Thanks.
Kenny Tangnde
08-Sep-2011Hi Awadhendra Tiwari ,
Please provide about how use datagridview control pop-up menu functions to right key.
Thanks in advance.